import http from '../../utils/request'
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        isShowlogin:{type:Boolean,value:''},
        linkadress:{type:String,value:''}
    },
    /**
     * 组件的初始数据
     */
    data: {
        ishowset:false,
        isLogin:false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        linkUrl:''
        // userInfo:{}
    },
    /**
     * 组件的方法列表
     */
    methods: {
        bindGetUserInfo(){
            var that = this
            this.setData({ishowset:false})
                wx.getUserProfile({
                  desc:"用户信息",
                  success(res){
                    wx.setStorage({
                        key: "third_Session", 
                        data: JSON.stringify(res)
                    })
                    wx.login({
                        success (ret) {
                            if (ret.code) {
                                const code = ret.code
                                console.info(code)
                                console.info(res.userInfo.nickName)
                                wx.request({
                                    url: 'https://www.bslmedu.com/Bslm/api/wx_program_login',
                                    method:'POST',
                                    data:{code:code,wxname:res.userInfo.nickName},
                                    header:{"content-type":"application/json;charset=UTF-8"},
                                    success: function (ress) {
                                        const self = this
                                        if (ress.data.code==0) {
                                          var json = JSON.stringify(ress.data.obj)
                                          wx.setStorage({
                                            key: "userInfo", 
                                            data: json
                                          })
                                          if(that.data.linkUrl==='mine'){
                                            wx.reLaunch({
                                                url: that.data.linkUrl
                                              })
                                          }
                                          wx.showToast({
                                                title: '登录成功',
                                                icon: 'success',
                                                duration: 2000
                                            })
                                        }else {
                            
                                        }
                                    },
                                    fail: function (res) {
                            
                                    }
                                })
                            } else {
                                wx.showToast({
                                    title: '登录失败！' + res.errMsg,
                                    icon: 'error',
                                    duration: 2000
                                })
                            }
                        }
                    })
                  },
                 fail(err){
                        wx.showToast({
                            title: '您拒绝了授权',
                            icon: 'error',
                            duration: 2000
                          })
                    }
                })
          }
    },
    observers:{
        'isShowlogin': function(val){
          this.setData({ishowset:val})
        },
        'linkadress':function(val){
            this.setData({linkUrl:val})
        }
      }
})
